package com.qf.web.controller;/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/18
 */

import cn.dsna.util.images.ValidateCode;
import com.qf.domain.Address;
import com.qf.domain.User;
import com.qf.service.AddressService;
import com.qf.service.UserService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.UserServiceImpl;
import com.qf.util.Base64Utils;
import com.qf.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:Project_2202
 * author:
 * time:2022/4/18 19:05
 * description:
 */
@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();
    public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        if(StringUtils.isEmpty(username)){
            response.getWriter().print("0");
            return;
        }
        boolean flag = userService.checkUsername(username);
        if(flag==true){
            response.getWriter().print("1");
        }else{
            response.getWriter().print("2");
        }

    }

    public String register(HttpServletRequest request,HttpServletResponse response)throws IOException{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(email)||StringUtils.isEmpty(password)){
            return "redirect:/register.jsp";
        }

        boolean flag = userService.checkUsername(username);
        if(flag) {
            return "redirect:/register.jsp";
        }

        boolean isSuccess = userService.register(username,password,email,gender);
        if(isSuccess){
            return "redirect:/login.jsp";
        }else{
            return "redirect:/register.jsp";
        }
    }

    public  void  code(HttpServletRequest request,HttpServletResponse response)throws IOException{
        ValidateCode validateCode = new ValidateCode(120, 50, 2, 5);
        String code = validateCode.getCode();
        request.getSession().setAttribute("code", code);

        validateCode.write(response.getOutputStream());
    }

    public void checkCode  (HttpServletRequest request,HttpServletResponse response)throws IOException{
        String code = request.getParameter("code");
        if(StringUtils.isEmpty(code)){
            response.getWriter().print("1");
        }

        String code1 = (String)request.getSession().getAttribute("code");
        if(code.equalsIgnoreCase(code1)){
            response.getWriter().print("0");
        }else{
            response.getWriter().print("1");
        }
    }
    public String login (HttpServletRequest request,HttpServletResponse response)throws IOException{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            return "redirect:/login.jsp";
        }

        User user = userService.login(username,password);

        if(user!=null){
            String auto = request.getParameter("auto");
            String userinfo = username+"#"+password;
            Cookie cookie = new Cookie("userinfo", Base64Utils.encode(userinfo));
            if(auto!=null){

                cookie.setMaxAge(2*7*24*60*60);


            }else{
                cookie.setMaxAge(0);
            }
            response.addCookie(cookie);
            request.getSession().setAttribute("user", user);
            return "redirect:/index.jsp";
        }else{
            return "redirect:/login.jsp";
        }
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String logOut(HttpServletRequest request,HttpServletResponse response)throws IOException{
        request.getSession().removeAttribute("user");
        request.getSession().invalidate();
        Cookie cookie = new Cookie("userinfo", Base64Utils.encode(""));
        //生存时间
        cookie.setMaxAge(1);
        cookie.setPath("/"); //在此网站任意路径可用
        response.addCookie(cookie);
        return "redirect/login.jsp";
    }
    AddressService addressService = new AddressServiceImpl();
    public String getAddress(HttpServletRequest request,HttpServletResponse response)throws IOException{

        User user = (User) request.getSession().getAttribute("user");

        List<Address> addList = addressService.findAddListByUid(user.getId());

        request.setAttribute("addList", addList);

        return "forward:/self_info.jsp";
    }
    public String addAddress(HttpServletRequest request,HttpServletResponse response)throws IOException{
        User user = (User) request.getSession().getAttribute("user");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        //如果为空
        if(StringUtils.isEmpty(name ) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(detail)) {
            System.out.println("数据有误，请重新添加");
            return "redirect:/userservlet?method=getAddress";
        }

        Address address = new Address();
        address.setDetail(detail);
        address.setLevel(0);
        address.setName(name);
        address.setPhone(phone);
        address.setUid(user.getId());

        addressService.insert(address);

        return "redirect:/userservlet?method=getAddress";
    }

    public String deleteAddress(HttpServletRequest request,HttpServletResponse response)throws IOException{

        String id = request.getParameter("id");
        addressService.deleteById(id);
        return "redirect:/userservlet?method=getAddress";
    }
    public String updateAddress(HttpServletRequest request,HttpServletResponse response)throws IOException{
        User user = (User) request.getSession().getAttribute("user");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        int level =Integer.parseInt( request.getParameter("level"));
        int id =Integer.parseInt(request.getParameter("id")) ;
        //如果为空
        if(StringUtils.isEmpty(name ) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(detail)) {
            System.out.println("数据有误，请重新添加");
            return "redirect:/userservlet?method=getAddress";
        }

        Address address = new Address();
        address.setDetail(detail);
        address.setLevel(level);
        address.setName(name);
        address.setPhone(phone);
        address.setUid(user.getId());
        address.setId(id);

        addressService.update(address);

        return "redirect:/userservlet?method=getAddress";
    }
    public String defaultAddress(HttpServletRequest request,HttpServletResponse response)throws IOException{
        User user = (User) request.getSession().getAttribute("user");
        String aid = request.getParameter("id");

        addressService.setDefaultAddress(aid, user.getId());

        return "redirect:/userservlet?method=getAddress";
    }
}